/**
 * 树节点
 * @param models 模型数组
 * @param projectName 项目名称
 * @param projectDescribe 项目描述
 * @param projectState 项目状态
 * @param projectId 项目ID
 * @returns 树节点数组
 */
function buildTreeData(
  models: ModelVO[],
  projectName: string,
  projectDescribe: string,
  projectState: string,
  projectId: string
): Tree[] {
  const rootNode: Tree = {
    id: projectId,
    parentId: '-1',
    label: projectName,
    describe: projectDescribe,
    state: projectState,
    children: models.filter((model) => model).map((model) => mapModelToTreeNode(model))
  }

  function mapModelToTreeNode(model: ModelVO): Tree {
    return {
      id: model.modelId,
      parentId: model.parentId,
      label: model.modelName,
      describe: model.modelDescribe,
      state: model.modelState,
      children: model.modelChild
        ? model.modelChild.filter((child) => child).map(mapModelToTreeNode)
        : []
    }
  }
  return [rootNode]
}
/**
 * 递归搜索树中的叶子节点，排除没有名字的节点
 * @param nodes 树节点数组
 * @returns 叶子节点数组
 */
function collectLeafNodes(nodes: Tree[]): Tree[] {
  let leaves: Tree[] = []
  nodes.forEach((node) => {
    if (node.children && node.children.length > 0) {
      leaves = leaves.concat(collectLeafNodes(node.children))
    } else {
      // 当节点没有子节点且具有有效名称时，认为它是叶子节点
      if (node.label) {
        leaves.push(node)
      }
    }
  })
  return leaves
}
export { buildTreeData, collectLeafNodes }
